//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.genmer.test.security.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.Map.Entry;

public class RequestArgumentBody implements IRequestArgumentBody {
    private static final long serialVersionUID = 1943543698297026560L;
    private JSONObject arguments = null;
    private JSONObject attributes = null;

    public RequestArgumentBody(Map<String, Object> arguments) {
        this.arguments = new JSONObject();
        this.arguments.putAll((Map)(null == arguments ? new LinkedHashMap(0) : arguments));
    }

    public RequestArgumentBody(JSONObject arguments) {
        this.arguments = null == arguments ? new JSONObject() : arguments;
    }

    public static IRequestArgumentBody createRequestArgument(HttpServletRequest request) {
        Enumeration parameterNames = request.getParameterNames();
        HashMap params = new HashMap();

        while(true) {
            while(parameterNames.hasMoreElements()) {
                String paramName = (String)parameterNames.nextElement();
                String[] paramValues = request.getParameterValues(paramName);
                String paramValue = request.getParameter(paramName);
                if (null != paramValues && 1 < paramValues.length) {
                    params.put(paramName, paramValues);
                } else {
                    params.put(paramName, paramValue);
                }
            }

            return new RequestArgumentBody(params);
        }
    }

    public static IRequestArgumentBody createRequestArgument(HttpServletRequest request, Object obj) {
        String jsonValue = JSONObject.toJSONString(obj);
        JSONObject jsonObject = JSONObject.parseObject(jsonValue);
        filter((Map)jsonObject);
        return new RequestArgumentBody(jsonObject);
    }

    public static IRequestArgumentBody createRequestArgument(HttpServletRequest request, String paramName) {
        String jsonValue = request.getParameter(paramName);
        if (null != jsonValue && jsonValue.startsWith("{")) {
            JSONObject jsonObject = JSONObject.parseObject(jsonValue);
            filter((Map)jsonObject);
            return new RequestArgumentBody(jsonObject);
        } else if (null != jsonValue && jsonValue.startsWith("[")) {
            JSONArray jsonArray = JSONArray.parseArray(jsonValue);
            filter(jsonArray);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data", jsonArray);
            return new RequestArgumentBody(jsonObject);
        } else {
            return null;
        }
    }

    public static IRequestArgumentBody createRequestArgument(Map<String, Object> arguments) {
        filter(arguments);
        return new RequestArgumentBody(arguments);
    }

    public String getStringValue(String paramName) {
        return this.getStringValue(paramName, (String)null);
    }

    public String getStringValue(String paramName, String defaultValue) {
        if (this.constians(paramName)) {
            String value = this.arguments.getString(paramName);
            return null == value ? defaultValue : value;
        } else {
            return defaultValue;
        }
    }

    public int getIntValue(String paramName) {
        return this.getIntValue(paramName, 0);
    }

    public int getIntValue(String paramName, int defaultValue) {
        if (this.constians(paramName)) {
            Integer value = this.arguments.getInteger(paramName);
            return null == value ? defaultValue : value;
        } else {
            return defaultValue;
        }
    }

    public long getLongValue(String paramName) {
        return this.getLongValue(paramName, 0L);
    }

    public long getLongValue(String paramName, long defaultValue) {
        if (this.constians(paramName)) {
            Long value = this.arguments.getLong(paramName);
            return null == value ? defaultValue : value;
        } else {
            return defaultValue;
        }
    }

    public boolean getBooleanValue(String paramName) {
        return this.getBooleanValue(paramName, false);
    }

    public boolean getBooleanValue(String paramName, boolean defaultValue) {
        if (this.constians(paramName)) {
            Boolean value = this.arguments.getBoolean(paramName);
            return null == value ? defaultValue : Boolean.valueOf(value.toString());
        } else {
            return defaultValue;
        }
    }

    public JSONObject getJSONObject(String paramName) {
        return this.constians(paramName) ? this.arguments.getJSONObject(paramName) : null;
    }

    public JSONArray getJSONArray(String paramName) {
        return this.constians(paramName) ? this.arguments.getJSONArray(paramName) : null;
    }

    public boolean constians(String paramName) {
        return this.arguments.containsKey(paramName);
    }

    public <T> T getObject(Class<T> clazz) {
        return JSONObject.parseObject(this.arguments.toJSONString(), clazz);
    }

    public <T> T getObject(String paramName, Class<T> clazz) {
        JSONObject object = this.getJSONObject(paramName);
        return null != object ? JSONObject.parseObject(object.toJSONString(), clazz) : null;
    }

    public <T> List<T> getObjects(Class<T> clazz) {
        return JSONArray.parseArray(this.arguments.getJSONArray("data").toJSONString(), clazz);
    }

    public <T> List<T> getObjects(String paramName, Class<T> clazz) {
        JSONArray array = this.getJSONArray(paramName);
        return (List)(null != array ? JSONArray.parseArray(array.toJSONString(), clazz) : new ArrayList(0));
    }

    public void setLoginUserId(long loginUserId) {
        this.arguments.put("loginUserId", loginUserId);
    }

    public long getLoginUserId() {
        return this.arguments.getLongValue("loginUserId");
    }

    public void setLoginAppCode(String loginAppCode) {
        this.arguments.put("loginAppCode", loginAppCode);
    }

    public String getLoginAppCode() {
        return this.arguments.getString("loginAppCode");
    }

    public boolean isSuperAdmin() {
        boolean superAdmin = false;
        if (this.constians("superAdmin")) {
            superAdmin = this.arguments.getBoolean("superAdmin");
        }

        return superAdmin;
    }

    public void addParameters(Map<String, Object> parameters) {
        if (null != parameters && !parameters.isEmpty()) {
            Iterator iterator = parameters.entrySet().iterator();

            while(iterator.hasNext()) {
                Entry<String, Object> next = (Entry)iterator.next();
                this.arguments.putIfAbsent(next.getKey(), next.getValue());
            }
        }

    }

    public void addParameter(String paramName, Object value) {
        if (null != paramName && !"".equals(paramName.trim()) && null != value) {
            this.arguments.putIfAbsent(paramName.trim(), value);
        }

    }

    public void addAttributes(Map<String, Object> attributes) {
        if (null == this.attributes) {
            this.attributes = new JSONObject();
        }

        this.attributes.putAll(attributes);
    }

    public void addAttribute(String attrName, Object value) {
        if (null == this.attributes) {
            this.attributes = new JSONObject();
        }

        this.attributes.put(attrName, value);
    }

    public Object getAttribute(String attrName) {
        return null != this.attributes && this.attributes.containsKey(attrName) ? this.attributes.get(attrName) : null;
    }

    public String getJsonString() {
        return this.arguments != null ? this.arguments.toJSONString() : null;
    }

    public Map<String, Object> getRequestParams() {
        Map<String, Object> params = new HashMap();
        if (null != this.arguments) {
            params.putAll(this.arguments);
        }

        if (null != this.attributes) {
            Iterator iterator = this.attributes.entrySet().iterator();

            while(iterator.hasNext()) {
                Entry<String, Object> entry = (Entry)iterator.next();
                params.putIfAbsent(entry.getKey(), entry.getValue());
            }
        }

        return params;
    }

    public int getPageIndex() {
        int pageIndex = 1;
        if (this.constians("pageIndex")) {
            pageIndex = this.arguments.getIntValue("pageIndex") + 1;
        }

        pageIndex = pageIndex < 1 ? 1 : pageIndex;
        return pageIndex;
    }

    public int getPageSize() {
        int pageSize = 50;
        if (this.constians("pageSize")) {
            pageSize = this.arguments.getIntValue("pageSize");
        }

        pageSize = pageSize < 1 ? 50 : pageSize;
        return pageSize;
    }

//    public <T> ResponsePagination<T> getPagination() {
//        return new ResponsePagination(this.getPageIndex(), this.getPageSize());
//    }

    private static void filter(JSONArray arguments) {
        int index = 0;

        for(int len = arguments.size(); index < len; ++index) {
            Object jsonObject = arguments.get(index);
            if (jsonObject instanceof JSONObject) {
                filter((Map)((JSONObject)jsonObject));
            }
        }

    }

    private static void filter(Map<String, Object> source) {
        if (null != source && !source.isEmpty()) {
            Iterator iterator = source.entrySet().iterator();

            while(iterator.hasNext()) {
                Entry<String, Object> entry = (Entry)iterator.next();
                String key = (String)entry.getKey();
                if (key.startsWith("_")) {
                    iterator.remove();
                } else {
                    Object value = entry.getValue();
                    if (value instanceof JSONObject) {
                        filter((Map)((JSONObject)value));
                    }

                    if (value instanceof JSONArray) {
                        filter((JSONArray)value);
                    }
                }
            }
        }

    }
}
